package com.example.service;

import com.example.entity.RegionStats;
import com.example.utils.DataObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author junqiancai@foxmail.com
 * @since 2021/5/15 23:17
 */
@Service
@Slf4j
public class ConfirmedService {
    //保存确诊信息
    List<RegionStats> list;

    public void updateList(){
        list = DataObject.getVirusConfirmedList();
    }
    //获取某个state的确诊情况
    public RegionStats getStateConfirmed(String stateName){
        List<RegionStats> collector = this.list.stream().filter(it -> it.getState().equals(stateName)).collect(Collectors.toList());
        if(collector.size()!=1){
            log.error("错误：获取到的state的记录不为1");
            return null;
        }
        return collector.get(0);
    }

    //获取某个country的确诊情况
    public RegionStats getCountryConfirmed(String countryName){
        Optional<RegionStats> res = this.list.stream()
                .filter(it -> it.getCountry().equals(countryName))
                .reduce((acc, it) -> {
                    //累加总的确诊数和新增数
                    acc.setLatestTotalCases(acc.getLatestTotalCases() + it.getLatestTotalCases());
                    acc.setDiffFromPrevDay(acc.getDiffFromPrevDay() + it.getDiffFromPrevDay());
                    //累加每一天的确诊数
                    ArrayList<Integer> amountList = acc.getAmountList();
                    ArrayList<Integer> amountList2 = it.getAmountList();
                    if(amountList2.size()!=amountList.size())log.error("不同region存在amountList长度不一致问题");
                    for(int i=0;i<amountList.size();i++){
                        amountList.set(i,amountList.get(i)+amountList2.get(i));
                    }
                    return acc;
                });
        if(!res.isPresent()){
            log.error("错误：不存在此country");
        }
        else {
            log.info(String.valueOf(res.get()));
            return  res.get();
        }
        return null;
    }

    //获取全球的确认情况
    public RegionStats getGlobalConfirmed(){
        Optional<RegionStats> res = this.list.stream()
                .reduce((acc, it) -> {
                    //累加总的确诊数和新增数
                    acc.setLatestTotalCases(acc.getLatestTotalCases() + it.getLatestTotalCases());
                    acc.setDiffFromPrevDay(acc.getDiffFromPrevDay() + it.getDiffFromPrevDay());
                    //累加每一天的确诊数
                    ArrayList<Integer> amountList = acc.getAmountList();
                    ArrayList<Integer> amountList2 = it.getAmountList();
                    if(amountList2.size()!=amountList.size())log.error("不同region存在amountList长度不一致问题");
                    for(int i=0;i<amountList.size();i++){
                        amountList.set(i,amountList.get(i)+amountList2.get(i));
                    }
                    return acc;
                });
        if(!res.isPresent()){
            log.error("错误：累计全球数据无结果");
        }
        else {
            log.info(String.valueOf(res.get()));
            return  res.get();
        }
        return null;
    }


}
